package org.csu.onlineedubackend.controller;

import org.csu.onlineedubackend.dto.ForumFollowDTO;
import org.csu.onlineedubackend.result.CommonResponse;
import org.csu.onlineedubackend.service.ForumFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 論壇關注控制器
 */
@RestController
@RequestMapping("/api/forum/follow")
public class ForumFollowController {

    @Autowired
    private ForumFollowService forumFollowService;

    /**
     * 關注用戶
     */
    @PostMapping("/{followingId}")
    public CommonResponse<Boolean> follow(@PathVariable Integer followingId, 
                                        @RequestParam Integer followerId) {
        try {
            boolean result = forumFollowService.follow(followerId, followingId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("關注失敗：" + e.getMessage());
        }
    }

    /**
     * 取消關注
     */
    @DeleteMapping("/{followingId}")
    public CommonResponse<Boolean> unfollow(@PathVariable Integer followingId, 
                                          @RequestParam Integer followerId) {
        try {
            boolean result = forumFollowService.unfollow(followerId, followingId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("取消關注失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢是否關注
     */
    @GetMapping("/check/{followingId}")
    public CommonResponse<Boolean> isFollowing(@PathVariable Integer followingId, 
                                             @RequestParam Integer followerId) {
        try {
            boolean result = forumFollowService.isFollowing(followerId, followingId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("查詢失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢關注列表
     */
    @GetMapping("/following/{userId}")
    public CommonResponse<List<ForumFollowDTO>> getFollowingList(@PathVariable Integer userId) {
        try {
            List<ForumFollowDTO> list = forumFollowService.getFollowingList(userId);
            return CommonResponse.success(list);
        } catch (Exception e) {
            return CommonResponse.error("查詢關注列表失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢粉絲列表
     */
    @GetMapping("/followers/{userId}")
    public CommonResponse<List<ForumFollowDTO>> getFollowerList(@PathVariable Integer userId) {
        try {
            List<ForumFollowDTO> list = forumFollowService.getFollowerList(userId);
            return CommonResponse.success(list);
        } catch (Exception e) {
            return CommonResponse.error("查詢粉絲列表失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢關注數
     */
    @GetMapping("/following-count/{userId}")
    public CommonResponse<Integer> getFollowingCount(@PathVariable Integer userId) {
        try {
            int count = forumFollowService.getFollowingCount(userId);
            return CommonResponse.success(count);
        } catch (Exception e) {
            return CommonResponse.error("查詢關注數失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢粉絲數
     */
    @GetMapping("/follower-count/{userId}")
    public CommonResponse<Integer> getFollowerCount(@PathVariable Integer userId) {
        try {
            int count = forumFollowService.getFollowerCount(userId);
            return CommonResponse.success(count);
        } catch (Exception e) {
            return CommonResponse.error("查詢粉絲數失敗：" + e.getMessage());
        }
    }
} 